package api;

import java.util.HashMap;
import java.util.Map;
import org.junit.Test;

import services.image.ImageSearchService.ImageExt;
import services.video.VideoFeed;
import controllers.Data;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Http.StatusCode;
import util.JsonParserHelper;
import base.BaseTest;
import play.Logger;

public class InternalSearchesOnInternet extends BaseTest {
	
	@Test
	public void searchTest() {
		//Loggin en el sistema de Juan Gomez familiar de Eva perez
		Map<String, String> loginUserParams = new HashMap<String, String>();
		loginUserParams.put("username", "juan.gomez@B");
		loginUserParams.put("password", "secret2");
		// Login here so the following request will be authenticated:
		Response response = POST("/login", loginUserParams);
		assertStatus(StatusCode.FOUND, response);
		cookies.putAll(response.cookies);
		Request request = newRequest();
        request.cookies.putAll(cookies);
		response = GET(request, "/api/me/internet/search?q=coches&offset=0&limit=8&type=images");
		assertStatus(StatusCode.OK, response);
		Data<ImageExt> images = JsonParserHelper.toImageCollection(response.out.toByteArray());
		assertTrue(images.total >= images.data.size());
		for(ImageExt image : images.data) {
			Logger.info("Title: %s original: %s thumb: %s", image.title, image.original, image.thumbnail);
		}
		request = newRequest();
        request.cookies.putAll(cookies);
		response = GET(request, "/api/me/internet/search?q=alfa+romeo&type=imagess");
		assertStatus(StatusCode.BAD_REQUEST, response);
		request = newRequest();
        request.cookies.putAll(cookies);
		response = GET(request, "/api/me/internet/search?q=alfa+romeo");
		assertStatus(StatusCode.BAD_REQUEST, response);
		request = newRequest();
        request.cookies.putAll(cookies);
		response = GET(request, "/api/me/internet/search?q=ferrari&type=videos&offset=0&limit=5");
		VideoFeed videos = JsonParserHelper.toVideoFeed(response.out.toByteArray());
		assertStatus(StatusCode.OK, response);
	}
	
}